/*
 * Copyright 2012 NJUT  qixiaobo. All rights reserved.
 */
package com.fujitsu.nanjing.tieba.service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fujitsu.nanjing.tieba.base.BaseService;
import com.fujitsu.nanjing.tieba.common.ReflectUtils;
import com.fujitsu.nanjing.tieba.domain.BoardBlobsInfo;
import com.fujitsu.nanjing.tieba.ibator.CoreBoard;
import com.fujitsu.nanjing.tieba.ibator.CoreBoardCriteria;
import com.fujitsu.nanjing.tieba.model.Board;

/**
 * BoardBlobsInfoService.java
 * 
 * @author Qixiaobo-win8
 */
@Service
@Transactional
public class BoardBlobsInfoService
		extends
			BaseService<BoardBlobsInfo, CoreBoardCriteria> {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(BoardBlobsInfoService.class);

	@Resource
	private Board board;
	@Resource
	private TopicService topicService;
	@Resource
	private ReplyService replyService;
	@Override
	public BoardBlobsInfo findById(String id) {

		CoreBoard coreBoard = board.selectByPrimaryKey(Integer.parseInt(id));
		if (coreBoard == null)
			return null;
		BoardBlobsInfo info = new BoardBlobsInfo();
		try {
			ReflectUtils.copy(coreBoard, info);
		} catch (IllegalAccessException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		info.setReplies(replyService.countRepliesByBoardId(Integer.parseInt(id)));
		info.setTopics(topicService.countByBoardId(Integer.parseInt(id)));
		info.setTopicsAndReplies(info.getReplies() + info.getTopics());
		info.setLastTopic(topicService.findLastTopicByBoardid(Integer
				.parseInt(id)));
		return info;

	}
	@Override
	public BoardBlobsInfo insert(BoardBlobsInfo t) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public void update(BoardBlobsInfo t) {
		// TODO Auto-generated method stub

	}
	@Override
	public void delete(BoardBlobsInfo t) {
		// TODO Auto-generated method stub

	}
	/**
	 * 
	 * 封装包含回复和主题的板块实体
	 * */
	public List<BoardBlobsInfo> findBySectionId(int sectionId) {
		CoreBoardCriteria example = new CoreBoardCriteria();
		example.createCriteria().andSectionidEqualTo(sectionId);

		List<CoreBoard> coreBoards = board.selectByExampleWithoutBLOBs(example);

		return init(coreBoards);

	}
	public List<BoardBlobsInfo> init(List<CoreBoard> coreBoards) {
		List<BoardBlobsInfo> infos = new ArrayList<BoardBlobsInfo>();
		BoardBlobsInfo info = null;
		for (CoreBoard coreBoard : coreBoards) {
			info = new BoardBlobsInfo();
			try {
				ReflectUtils.copy(coreBoard, info);
			} catch (IllegalAccessException e) {
				logger.error(e.getMessage());
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				logger.error(e.getMessage());
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				logger.error(e.getMessage());
				e.printStackTrace();
			}
			info.setReplies(replyService.countRepliesByBoardId(coreBoard
					.getBoardid()));
			info.setTopics(topicService.countByBoardId(coreBoard.getBoardid()));
			info.setTopicsAndReplies(info.getReplies() + info.getTopics());
			info.setLastTopic(topicService.findLastTopicByBoardid(coreBoard
					.getBoardid()));
			info.setNewTopics(topicService.countNewTopicsByBoardId(coreBoard
					.getBoardid()));
			infos.add(info);
		}
		return infos;
	}
	public List<BoardBlobsInfo> findBoardsWithOrder(String order) {
		List<CoreBoard> coreBoards = null;
		if (order.equalsIgnoreCase("topics")) {
			coreBoards = board.getOrderdByTopics();
		} else {
			coreBoards = board.getOrderdByReplies();
		}

		return init(coreBoards);

	}
}
